package com.partner.boot.common;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
import com.partner.boot.entity.User;
import com.partner.boot.mapper.UserMapper;
import com.partner.boot.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName InitRunner
 * @Description 配置启动时的初始化操作
 * @Author 刘孟坤
 * @Date 2023/8/11 0:21
 * Version 1.0
 **/
@Component
@Slf4j
public class InitRunner implements ApplicationRunner {

    @Autowired
    IUserService userService;

    @Resource
    UserMapper userMapper;

    /**
     * @description: 在项目启动后运行此方法
     * @author 刘孟坤
     * @date: 2023/8/11 0:22
     * @param: args
     * @return: void
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //在项目启动时查询一次数据库，防止数据库的懒加载

        //发送一次web请求，来初始化tomcat
        ThreadUtil.execAsync(() -> {
            try {
                userMapper.select1();
                log.info("启动项目tomcat连接查询成功");
                HttpUtil.get("http://localhost:9090/goodfirstlogin?username=0");
            } catch (Exception e) {
                log.warn("启动优化失败", e);
            }
        });
    }
}